﻿using System;
using System.Data;
using System.Configuration;
using System.Web;

using System.Data.OleDb;
using System.Data.SqlClient;
using BookStore.Data;

/// <summary>
/// Summary description for GioHang
/// </summary>
public class GioHangData
{
    DataTable dt;
	public GioHangData()
	{
        dt = new DataTable();
        dt.Columns.Add("image");
        dt.Columns.Add("name");
        dt.Columns.Add("num");
        dt.Columns.Add("price");
        dt.Columns.Add("total");       
        dt.Columns.Add("id");
	}

    public DataTable  setTable()
    {
        return this.dt;
    }

    public DataTable Writetable(DataTable tb, string image, string name, double num, double price, string id)
    {
        if (tb.Rows.Count != 0)
        {
            bool test = false;
            foreach (DataRow dr in tb.Rows)
            {
                if (dr[1].ToString().Equals(name))
                {
                    dr[2] = Convert.ToDouble(dr[2].ToString()) + 1;
                    dr[4] = Convert.ToDouble(dr[2].ToString()) * price;
                    test = true;
                }
            }
            if (!test)
            {
                DataRow dr = tb.NewRow();
                dr[0] = image;
                dr[1] = name;
                dr[2] = num;
                dr[3] = price;
                dr[4] = num * price;
                dr[5] = id;   
                tb.Rows.Add(dr);
            }
        }
        else
        {
            DataRow dr = tb.NewRow();
            dr[0] = image;
            dr[1] = name;
            dr[2] = num;
            dr[3] = price;
            dr[4] = num * price;
            dr[5] = id;            
            tb.Rows.Add(dr);
        }
        return tb;
    }

    public DataTable Delrows(DataTable tb, string name)
    {
        foreach (DataRow dr in tb.Rows)
        {
            if (dr[1].ToString().Equals(name))
            {
                tb.Rows.Remove(dr);
                break;
            }
        }
        return tb;
    }

    public DataTable Update(DataTable tb, double num, string name)
    {
        DataTable td = tb;
        foreach (DataRow dr in td.Rows)
        {
            if (name.Trim().Equals(dr[1].ToString().Trim()))
            {
                dr[2] = num;
                dr[4] = double.Parse(dr[3].ToString()) * num;
            }
        }
        return td;
    }
    //public SqlConnection conn;

    //public string connectdata()
    //{
    //    string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["WebInetConnectionString"].ToString();

    //    conn = new SqlConnection(connectionString);
    //    try
    //    {
    //        if (conn.State != ConnectionState.Open)
    //        {
    //            conn.Open();
    //        }

    //        return "true";
    //    }
    //    catch (Exception ex)
    //    {
    //        return ex.Message;
    //    }
    //    finally
    //    {
    //        //  conn.Close();
    //    }

    //}
    static SqlConnection con = sqlDataProvider.getConnect();
    public DataTable Gettable(string sql, string nameTable)
    {
        
        //@@@ tra ve chua het , neu null thi sao , neu loi sql thi sao ????

        DataTable tblGet = new DataTable();
        try
        {
                SqlDataAdapter da = new SqlDataAdapter(sql, con);
                DataSet ds = new DataSet();
                da.Fill(ds, nameTable);
                //???@@@ tai sao ten cua datatable va dataset phai trung nhau thi moi tra ve ket qua
                tblGet = ds.Tables[nameTable];

            return tblGet;
        }
        catch (SqlException)
        {
            return tblGet;
            // Console.WriteLine(sqle.ToString());
            //khong can tra ve vi neu co chuyen gi thi datatable se null
        }
        finally
        {
            con.Close();
        }
    }
}
